package com.cgbl.cg.view

import android.content.Context
import android.text.TextUtils
import android.view.WindowManager
import android.widget.PopupWindow
import androidx.appcompat.widget.AppCompatEditText
import androidx.core.widget.doOnTextChanged
import androidx.recyclerview.widget.RecyclerView
import com.cgbl.cg.R
import com.cgbl.cg.adapter.WorkCommentAdapter
import com.lxj.xpopup.core.BottomPopupView
import com.robot.base.model.CommentBean
import com.robot.base.view.CgTextView

class WorkCommentPop(context: Context, var list: MutableList<CommentBean>) :
    BottomPopupView(context) {


    private var confirmClickListener: (id: String, content: String) -> Unit = { id, content -> }
    private var detailClickListener: (bean: CommentBean) -> Unit = {}

    private lateinit var editContent: AppCompatEditText
    private lateinit var tvConfirm: CgTextView
    private lateinit var mRvCollect: RecyclerView
    private lateinit var workCommentAdapter: WorkCommentAdapter

    override fun getImplLayoutId(): Int {
        return R.layout.app_pop_work_comment
    }

    override fun initPopupContent() {
        super.initPopupContent()
        activity.window.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        activity.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        tvConfirm = findViewById(R.id.tv_confirm)
        mRvCollect = findViewById(R.id.mRv)

        editContent = findViewById(R.id.edit_content)
        workCommentAdapter = WorkCommentAdapter()
        mRvCollect.adapter = workCommentAdapter
        workCommentAdapter.setNewInstance(list)

        workCommentAdapter.setOnItemClickListener { adapter, view, position ->
            detailClickListener.invoke(workCommentAdapter.data[position])
        }

        editContent.doOnTextChanged { text, start, before, count ->
            tvConfirm.isEnabled = !TextUtils.isEmpty(text)
        }

        tvConfirm.setOnClickListener {
            confirmClickListener.invoke(
                "", editContent.text.toString()
            )
        }
    }

    fun setData(dataList: MutableList<CommentBean>) {
        workCommentAdapter.setNewInstance(dataList)
        editContent.setText("")
    }

    fun setConfirmClickListener(click: (id: String, content: String) -> Unit): WorkCommentPop {
        confirmClickListener = click
        return this
    }

    fun setDetailClickListener(click: (bean: CommentBean) -> Unit): WorkCommentPop {
        detailClickListener = click
        return this
    }
}